<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="http://unpkg.com/vue@next"></script>
</head>

<body>
    <div id="root"></div>
</body>
<script>
    let app = Vue.createApp({
        template: '<child ></child>',
        setup() {
            const { provide, ref } = Vue
            const name = ref('aa')
            provide('name', name)
            provide('changeName', (value) => {
                name.value = value
            })
        },
    })
    app.component('child', {
        template: '<div @click=hanldeChange>{{name}}</div>',
        setup(props, context) {
            const { inject } = Vue
            // const name = inject('name', 'hello') //第二个参数是默认值如果没有则默认值
            const name = inject('name') //第二个参数是默认值如果没有则默认值
            const changeName = inject('changeName')
            const hanldeChange = () => {
                changeName('bb')
            }
            return { name, hanldeChange }
        },
    })
    app.mount('#root')
</script>

</html>